﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class TablesDocument : Document
    {
        #region Internal
        //Constructor
        internal TablesDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Tables;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "tables.html";
            _database = database;
        }
        //Properties
        //Methods
        internal override string DisplayName { get { return "Tables"; } }
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.createdOnColumn { width: 15%; }");

                AddCssLine(@"      	.rowCountColumn { width: 10%; }");
                AddCssLine(@"      	.dataSizeColumn { width: 10%; }");
                AddCssLine(@"      	.indexSizeColumn { width: 10%; }");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Tables</h2>");

                Boolean evenRow = true;
                var schemas = (from schema in _database.Schemas
                               orderby schema.Name
                               select schema);
                foreach (SqlServer.Schema schema in schemas)
                {
                    var tables = (from table in schema.Tables
                                  where table.Type == SqlServer.DatabaseObjectType.UserTable
                                  orderby table.Name
                                  select table);
                    if (tables.Count() != 0)
                    {
                        evenRow = true;

                        AddBodyLine(@"<h2>");
                        AddBodyLine(@"<img id=""img_" + schema.Name + @""" src=""" + RootFolderPath + @"minus.jpg""> ");
                        AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('" + schema.Name + @"');"">" + schema.Name + @"</a>");
                        AddBodyLine(@"</h2>");

                        AddBodyLine(@"<div id=""" + schema.Name + @""">");
                        AddBodyLine(@"<table>");

                        string header = @"<tr><th>Name</th><th>Created On</th><th>Row Count</th><th>Data Size (KB)</th>";
                        header += @"<th>Index Size (KB)</th><th>Description</td></tr>";
                        AddBodyLine(header);

                        string colGroup = @"<colgroup><col class=""nameColumn"" /><col class=""createdOnColumn"" />" +
                            @"<col class=""rowCountColumn"" />" +
                            @"<col class=""dataSizeColumn"" /><col class=""indexSizeColumn"" />" +
                            @"<col class=""descriptionColumn"" /></colgroup>";
                        AddBodyLine(colGroup);

                        evenRow = true;
                        foreach (SqlServer.Table table in tables)
                        {
                            StringBuilder sb = new StringBuilder();

                            if (evenRow == true)
                                sb.Append(@"<tr class=""evenRow"">");
                            else
                                sb.Append(@"<tr class=""oddRow"">");
                            evenRow = !evenRow;

                            string href = @"<td class=""name"">";
                            href += @"<a href=""http://index." + RootDatabase.Name 
                                + "." + table.Schema.Name + ".table."
                                + table.Name + @".html"">" + ScrubHtml(table.Name) + @"</a>";
                            href += @"</td>";
                            sb.Append(href);

                            sb.Append(@"<td>" + table.CreateDate.ToString() + @"</td>");
                            sb.Append(@"<td>" + AddCommasToNumericString(table.RowCount.ToString()) + @"</td>");
                            sb.Append(@"<td>" + AddCommasToNumericString(table.DataSpaceUsed.ToString()) + @"</td>");
                            sb.Append(@"<td>" + AddCommasToNumericString(table.IndexSpaceUsed.ToString()) + @"</td>");
                            
                            if (table.Description == "")
                                sb.Append(@"<td>&nbsp;</td></tr>");
                            else
                                sb.Append(@"<td>" + ScrubHtml(table.Description) + @"</td></tr>");

                            AddBodyLine(sb.ToString());
                        }
                        AddBodyLine(@"</table>");
                        AddBodyLine(@"</div>");
                    }
                }

                AddBodyLine(@"</div>");

                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
